package com.server.Service;

import com.server.Example.MoviesTypeExample;
import com.server.entity.MoviesType;
import com.server.mapper.MoviesTypeMapper;
import org.apache.ibatis.session.SqlSession;
import untils.MybatisClass;
import untils.Utils;

import java.util.ArrayList;
import java.util.Random;

/**
 * The type Movies type service.
 * Movie type service related implementation classes
 *
 * @author user
 */
public class MoviesTypeService {
    /**
     * Select vales' movie genres.
     * Filter out the list of movie genres that are still displayed
     *
     * @return String JsonList Calling method:moviesTypeService.selectiveGenres()
     */
    public static ArrayList<MoviesType> selectiveGenres(){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        MoviesTypeMapper moviesTypes = sqlSession.getMapper(MoviesTypeMapper.class);
        MoviesTypeExample moviesTypeExample = new MoviesTypeExample();
        moviesTypeExample.createCriteria().andMoviesTypeIsShowEqualTo(true);
        ArrayList<MoviesType> moviesTypesList = (ArrayList<MoviesType>) moviesTypes.selectByExample(moviesTypeExample);
        sqlSession.close();
        /*return JSON.toJSONString(moviesList);*/
        return moviesTypesList;
    }

    /**
     * Instr vales movies type int.
     *
     * @param number    the number
     * @param startTime the start time
     * @param entire    the entire
     * @return the int Calling method:moviesTypeService.instrValesMoviesType(number,startTime,entire)
     */
    public static int instrValesMoviesType(int number, String startTime, String entire){
        int sumCount = 0;
        SqlSession sqlSession=MybatisClass.getSqlSession();
        MoviesTypeMapper moviesTypeMapper = sqlSession.getMapper(MoviesTypeMapper.class);
        for (int i = 0; i <number ; i++) {
            sumCount+=moviesTypeMapper.insert(new MoviesType(Utils.getRandomString(10)+i, new Random().nextBoolean(),Utils.randomDate(startTime, entire)));
            sqlSession.commit();
        }
        sqlSession.close();
        return sumCount;
    }

    /**
     * Select by primary key movies type.
     *
     * @param id the id
     * @return the movies type Calling method:moviesTypeService.selectByPrimaryKey(id)
     */
    public static MoviesType selectByPrimaryKey(Integer id){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        MoviesTypeMapper moviesTypeMapper = sqlSession.getMapper(MoviesTypeMapper.class);
        MoviesType mimetype=moviesTypeMapper.selectByPrimaryKey(id);
        sqlSession.close();
        return mimetype == null ?new MoviesType():mimetype;
    }
}
